package com.gjy.util.poi;

import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.poi.poifs.filesystem.FileMagic;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-14 09:27:14
 */
public class EasyExcelUtil {

    public static ExcelTypeEnum detectWithFileMagic(String file) throws IOException {
        InputStream magics = FileMagic.prepareToCheckMagic(Files.newInputStream(Paths.get(file)));
        FileMagic magic = FileMagic.valueOf(magics);
        if (magic == FileMagic.OLE2) {
            return ExcelTypeEnum.XLS;
        } else if (magic == FileMagic.OOXML) {
            return ExcelTypeEnum.XLSX;
        }
        throw new IllegalArgumentException("Not an Excel file");
    }

}
